package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }
    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    //保存或更新
    @Override
    public void saveOrUpdate(StrategyDetail detail,String tags) {
        //目的地
        //根据攻略id拿到攻略对象,攻略对象里面有目的地对象
        Strategy strategy = strategyService.get(detail.getStrategy().getId());
        detail.setDest(strategy.getDest());
        //作者 暂时没有

        //摘要
        //1.由运营人员来处理
        //2.程序员,截取内容前200个字
        String content = detail.getStrategyContent().getContent();
        if(content.length() > 200){
            detail.setSummary(content.substring(0,200));
        }else{
            detail.setSummary(content);
        }

        //是否是国外
        List<Destination> toasts = destinationService.getToasts(strategy.getDest().getId());
        if(toasts.get(0).getId() != 1){
            detail.setIsabroad(StrategyDetail.ABROAD_YES);
        }else{
            detail.setIsabroad(StrategyDetail.ABROAD_NO);
        }


        if(detail.getId() == null){
            //创建时间 只有新增的时候才会有创建时间
            detail.setCreateTime(new Date()); //新增
            strategyDetailMapper.insert(detail);

            //内容
            StrategyContent strategyContent = detail.getStrategyContent();
            strategyContent.setId(detail.getId());
            strategyContentMapper.insert(strategyContent);

        }else{
            strategyDetailMapper.updateByPrimaryKey(detail); //更新

            //内容
            StrategyContent strategyContent = detail.getStrategyContent();
            strategyContent.setId(detail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);

            //先删除关系表再去做操作
            strategyDetailMapper.deleteRelation(detail.getId());
        }

        //---------------------------------------------------------

        //操作标签
        if(StringUtils.hasLength(tags)){
            String[] tagArr = tags.split(",");
            if(tagArr.length > 0){
                for (String s : tagArr) {
                    StrategyTag strategyTag = new StrategyTag();
                    strategyTag.setName(s);
                    strategyTagService.saveOrUpdate(strategyTag);

                    //维护多与多关系,中间表
                    strategyDetailMapper.insertRelation(detail.getId(),strategyTag.getId());
                }
            }
        }

    }

    //根据id查询内容
    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    //根据目的地id查询点击量前3的明细
    @Override
    public List<StrategyDetail> getViewnumTop3(Long id) {
        return strategyDetailMapper.getViewnumTop3(id);
    }

    //从redis中持久化vo对象到mysql中
    @Override
    public void updateStatisVo(StrategyStatisPersistenceVO vo) {
        strategyDetailMapper.updateStatisVo(vo);
    }
}
